package com.app.Timer;

import com.app.model.main.Article;
import com.app.model.main.RewardArticle;
import com.app.model.main.TopAndCoinArticle;
import com.app.model.main.UserInfo;
import com.app.repository.interfaces.ArticleMapper;
import com.app.repository.interfaces.RewardArticleMapper;
import com.app.repository.interfaces.TopAndCoinArticleMapper;
import com.app.repository.interfaces.UserInfoMapper;
import com.app.support.utils.RedisUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by cj_so on 2018/5/4.
 */
@Component
public class ArticleTimer {
    private final Logger logger = Logger.getLogger(ArticleTimer.class);

    @Autowired
    private TopAndCoinArticleMapper topAndCoinArticleMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private RewardArticleMapper rewardArticleMapper;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 将所有过期的top帖的状态设为失效或金币帖，时间间隔 2小时
     */
    @Scheduled(fixedRate = 2*60*60*1000)
    public void setTopArticleStatus(){
        List<Long> topIds = articleMapper.getExpiredArticle(Byte.valueOf("8"),String.valueOf(System.currentTimeMillis() -24*60*60*1000));
        for(long articleId :topIds){
            TopAndCoinArticle topAndCoinArticle = topAndCoinArticleMapper.getByArticleId(articleId);

            byte status = topAndCoinArticle.getRemainCoin()>0 ?(byte)2:(byte)0; //0-普通 1-置顶 2-金币
            topAndCoinArticleMapper.updateArticleType(topAndCoinArticle.getId(),status);

            byte oldStatus = articleMapper.selectByPrimaryKey(articleId).getType();
            articleMapper.updateArticleType(articleId,status == (byte)0?(byte)(oldStatus-8):(byte)(oldStatus+8));
        }
    }

    /**
     * 每天定时（凌晨）将所有过期悬赏帖的状态重置
     */
    /*@Scheduled(cron = "00 00 00 * * ?")
    @Transactional(propagation= Propagation.REQUIRED,rollbackFor = Exception.class,readOnly = false)
    public void makeConsistent(){
        //TODO 悬赏帖多长时间过期
        List<Long> rewardIds = articleMapper.getExpiredArticle(Byte.valueOf("32"),String.valueOf(System.currentTimeMillis() -24*60*60*1000));
        for(long articleId :rewardIds){

            RewardArticle rewardArticle = rewardArticleMapper.getByArticleId(articleId);
            rewardArticleMapper.updateStatus(articleId,(byte)2);

            Article article = articleMapper.selectByPrimaryKey(articleId);

            int coin = userInfoMapper.getUserCoin(article.getUserId());

            userInfoMapper.updateUserCoin(article.getUserId(),(long)(coin+rewardArticle.getCoin()));
            if(redisUtil.exists("userInfo" + article.getUserId())){
                UserInfo userInfo = redisUtil.get("userInfo" + article.getUserId());
                userInfo.setCoin(coin+rewardArticle.getCoin());
                redisUtil.set("userInfo" + article.getUserId(),userInfo);
            }


            *//*byte oldStatus = article.getType();
            articleMapper.updateArticleType(articleId,(byte)(oldStatus-32));*//*
        }
    }*/

}
